//@version=5
indicator("Smart Trail Signals NO CONDITIONS", overlay=true)

// Smart Trail Parameters
length = input.int(14, title="Smart Trail Length", minval=1, maxval=100)
multiplier = input.float(2.0, title="Smart Trail Multiplier", minval=0.1, maxval=10.0, step=0.1)
source = input.source(close, title="Smart Trail Source")
sensitivity = input.int(3, title="Smart Trail Sensitivity (1-5)", minval=1, maxval=5)

// Smart Trail Logic - Continuous operation
var float trail_up = na
var float trail_down = na
var int smart_trend = 1

// Calculate ATR for smart trail volatility adjustment
atr = ta.atr(length)
volatility_factor = atr * multiplier * (sensitivity / 3.0)

// Calculate basic trailing levels
basic_up = source - volatility_factor
basic_down = source + volatility_factor

// Smart Trail UP (Support)
trail_up := source > nz(trail_up[1]) and source[1] > nz(trail_up[1]) ? 
           math.max(nz(trail_up[1]), basic_up) : basic_up

// Smart Trail DOWN (Resistance)
trail_down := source < nz(trail_down[1]) and source[1] < nz(trail_down[1]) ? 
             math.min(nz(trail_down[1]), basic_down) : basic_down

// Determine smart trend direction
smart_trend := source > nz(trail_down[1]) ? 1 : source < nz(trail_up[1]) ? -1 : nz(smart_trend[1], 1)

// Smart Trail line
smart_trail_value = smart_trend == 1 ? trail_up : trail_down

// Smart trend change detection
smart_trend_changed = smart_trend != nz(smart_trend[1]) and bar_index > 1

// Candle coloring logic based on Smart Trail
bullish = close > smart_trail_value and smart_trend == 1
bearish = close < smart_trail_value and smart_trend == -1

// Define colors
bullishColor = color.new(color.green, 20)
bearishColor = color.new(color.red, 20)
neutralColor = color.new(color.gray, 40)

// Color candles based on trend
candleColor = bullish ? bullishColor : 
              bearish ? bearishColor : neutralColor

barcolor(candleColor)

// Plot Smart Trail
trail_color = smart_trend == 1 ? color.new(color.lime, 0) : color.new(color.red, 0)
plot(smart_trail_value, title="Smart Trail", color=trail_color, linewidth=2, display=display.pane)

// Smart Trail signals - all hours
smartTrailBull = smart_trend_changed and smart_trend == 1
smartTrailBear = smart_trend_changed and smart_trend == -1

// Plot Smart Trail signals
plotshape(smartTrailBull, style=shape.diamond, location=location.belowbar, color=color.new(color.lime, 30), size=size.small, title="Smart Trail Bull")
plotshape(smartTrailBear, style=shape.diamond, location=location.abovebar, color=color.new(color.red, 30), size=size.small, title="Smart Trail Bear")

// Alert conditions
alertcondition(smartTrailBull, title="Smart Trail Bull Signal", message="Smart Trail: Bullish trend change signal")
alertcondition(smartTrailBear, title="Smart Trail Bear Signal", message="Smart Trail: Bearish trend change signal")